package main

import (
	"errors"
	"fmt"
	"strconv"
)

type MyStack struct {
	maxSize int
	top int
	arr [5]int
}

//压栈
func (s *MyStack) push (val int) (err error) {
	if s.top == s.maxSize {
		err = errors.New("栈满")
		return
	}
	s.arr[s.top] = val
	s.top++
	return
}

//弹栈
func (s *MyStack) pop () (data int, err error){
	if s.top == 0 {
		err = errors.New("栈空")
		return
	}
	s.top--
	data = s.arr[s.top]
	return
}

//打印栈
func (s *MyStack) showStack () {
	for i:=0;i<s.top;i++ {
		fmt.Println(s.arr[i])
	}
}

func main(){
	s := MyStack{
		maxSize: 5,
		top:     0,
		arr:     [5]int{},
	}
	var flag = true
	for flag{
		fmt.Println("使用栈：")
		fmt.Println("输入push压栈")
		fmt.Println("输入pop弹栈")
		fmt.Println("输入show展示栈")
		fmt.Println("输入exit退出")
		var commend string
		var value string
		_, _ = fmt.Scanln(&commend)
		switch commend {
		case "push":
			fmt.Print("输入要添加的元素:")
			_, _ = fmt.Scanln(&value)
			data, err := strconv.Atoi(value)
			if err != nil {
				fmt.Println("请输入正整数")
			}else{
				err = s.push(data)
				if err != nil {
					fmt.Println("err=",err)
				}
			}
		case "pop":
			data,err := s.pop()
			if err != nil {
				fmt.Println("err=",err)
			}else{
				fmt.Println("data=", data)
			}
		case "show":
			s.showStack()
		case "exit":
			flag = false
		default:
			fmt.Println("请输入正确的选项")
		}
	}
}

